import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

import '../Gesture/GestureDetectorConflictTest.dart';
import '../Gesture/GestureDetectorHitTest.dart';
import '../Gesture/NotificationInterruptTest.dart';
import 'HttpDioUse.dart';
import 'HttpDioUseDownloadProgress.dart';
import 'HttpSetting.dart';
import 'WebSocketUserRoute.dart';
import 'WebviewFlutter.dart';

class FileOperatorLearing extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Material(
      child: SingleChildScrollView(
        child: Column(
          children: [
            TextButton(
              child: Text("基本的文件流创建流程"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return FileOperationRoute();
                }));
              },
            ),
            TextButton(
              child: Text("网络请求"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return HttpSetting();
                }));
              },
            ),
            TextButton(
              child: Text("Dio框架式的网络请求"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return HttpDioUse();
                }));
              },
            ),
            TextButton(
              child: Text("Dio框架式的网络请求,文件进度1"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return HttpDioUseDownloadProgressWidget();
                }));
              },
            ),
            TextButton(
              child: Text("WebSocket的网络请求"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                  return WebSocketRoute();
                }));
              },
            ),
          ],
        ),
      ),
    );
  }
}

///我们还是以计数器为例，实现在应用退出重启后可以恢复点击次数。 这里，我们使用文件来保存数据：
///
/// 本示例只是为了演示文件读写，而在实际开发中，如果要存储一些简单的数据，使用shared_preferences插件会比较简单。
class FileOperationRoute extends StatefulWidget {
  FileOperationRoute({Key? key}) : super(key: key);

  @override
  _FileOperationRouteState createState() => _FileOperationRouteState();
}

class _FileOperationRouteState extends State<FileOperationRoute> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    //从文件读取点击次数
    _readCounter().then((int value) {
      setState(() {
        _counter = value;
      });
    });
  }

  ///path_provider: ^2.0.2 需要该包支持
  Future<File> _getLocalFile() async {
    // 获取应用目录
    String dir = (await getApplicationDocumentsDirectory()).path;
    return File('$dir/counter.txt');
  }

  Future<int> _readCounter() async {
    try {
      File file = await _getLocalFile();
      // 读取点击次数（以字符串）
      String contents = await file.readAsString();
      return int.parse(contents);
    } catch (FileSystemException) {
      return 0;
    }
  }

  _incrementCounter() async {
    setState(() {
      _counter++;
    });
    // 将点击次数以字符串类型写到文件中
    await (await _getLocalFile()).writeAsString('$_counter');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('文件操作')),
      body: Center(
        child: Text('点击了 $_counter 次'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}
